package com.hellogood.yaohuo.server.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.data.CallDTO;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.PdForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class PdFormDAO extends FormDAO {
	Logger log = Logger.getLogger(this.getClass());

	public String createPandian(PdForm form, boolean isModify) {
		String result = "";
		List<String> sqls = new ArrayList<String>();
		if (isModify) {
			String sql = "DELETE FROM PdForm WHERE pdBillNo = '" + form.getBillNo() + "'";
			sqls.add(sql);
			sql = "DELETE FROM PdDetail WHERE pdBillNo = '" + form.getBillNo() + "'";
			sqls.add(sql);
		}
		String sql = "INSERT INTO PdForm(pdBillNo,pdDate,orgCode,counts,staffCode,staffName,rzDate,remark)" + " VALUES('" + form.getBillNo()
				+ "',CONVERT(varchar(100), GETDATE(), 23),'" + form.getOrgCode() + "'," + form.getCounts() + ",'" + form.getStaffCode() + "','"
				+ form.getStaffName() + "','" + form.getRzDate() + "','" + form.getRemark() + "')";
		sqls.add(sql);

		List<Detail> list = form.getList();
		if (list != null && (!list.isEmpty())) {
			for (Detail d : list) {
				sql = "INSERT INTO PdDetail(pdBillNo,pluCode,pluName,barCode,bra,color,size_s,size_m,size_l,size_xl,size_xxl,size_xxxl,size_avg,Counts,rzDate,remark,orgCode,depCode,depName) VALUES('"
						+ d.getBillNo()
						+ "','"
						+ d.getPluCode()
						+ "','"
						+ d.getPluName()
						+ "','"
						+ d.getBarCode()
						+ "','"
						+ d.getBra()
						+ "','"
						+ d.getColor()
						+ "',"
						+ d.getSize_s()
						+ ","
						+ d.getSize_m()
						+ ","
						+ d.getSize_l()
						+ ","
						+ d.getSize_xl()
						+ ","
						+ d.getSize_xxl()
						+ ","
						+ d.getSize_xxxl()
						+ ","
						+ d.getSize_avg()
						+ ","
						+ d.getCounts()
						+ ",'"
						+ d.getRzDate()
						+ "','"
						+ d.getRemark() + "','" + d.getOrgCode() + "','" + d.getDepCode() + "','" + d.getDepName() + "')";
				sqls.add(sql);
			}
		}
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	public String rz(String bill) throws DatabaseException {
		CallDTO dto = new CallDTO();
		String sql = "UPDATE pdForm SET rzDate = CONVERT(VARCHAR(10),getDate(),120) WHERE PdBillNo = '" + bill + "'";
		String call = "{CALL pdrz(?,?)}";
//		String call = "{EXEC pdrz @pdbillNo = '" + bill + "',@sysdb='" + DBConnection.hxDbName + "'}";
		dto.setBill(bill);
		dto.setCall(call);
		dto.setSql(sql);
		dto.setHxdbName(DBConnection.hxDbName);
		return super.callProcedure(dto);
	}

	@SuppressWarnings("unchecked")
	public List<Form> searchForm(String tableFormName, String billNoField, String dateField, String orgCode, String startDate, String endDate,
			String rz) {
		String hxDbName = DBConnection.hxDbName;
		String sql = "SELECT (SELECT OrgName FROM " + hxDbName + "..SubShop WHERE OrgCode = f.OrgCode) AS OrgName,f.*,f." + billNoField
				+ " AS billNo,f." + dateField + " AS billDate FROM " + tableFormName + " f WHERE f.OrgCode = '" + orgCode + "' AND cast(f."
				+ dateField + " as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + startDate + "'),120) and cast(f." + dateField
				+ " as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + endDate + "'),120)";
		if (rz.equals(Constants.RUZHANG_CODE_YES)) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals(Constants.RUZHANG_CODE_NO)) {
			sql = sql + " AND rzDate = ''";
		}
		List<?> list = super.query(sql);
		return (List<Form>) list;
	}

	@SuppressWarnings("unchecked")
	public List<Form> searchFormByQuyu(String tableFormName, String billNoField, String dateField, String quyuCode, String startDate, String endDate,
			String rz) {
		String hxDbName = DBConnection.hxDbName;
		String userCode = MainFrame.currentUser.getStaffCode();
		String orgs = "";
		if (quyuCode == null) {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUser(userCode);
		} else {
			orgs = MgrFactory.getInstance().createOrgMgr().getOrgCodeByUserQuyu(userCode, quyuCode);
		}

		String sql = "SELECT (SELECT OrgName FROM " + hxDbName + "..SubShop WHERE OrgCode = f.OrgCode) AS OrgName,f.*,f." + billNoField
				+ " AS billNo,f." + dateField + " AS billDate FROM " + tableFormName + " f WHERE f.OrgCode IN (" + orgs + ") AND cast(f." + dateField
				+ " as char(10)) >= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + startDate + "'),120) and cast(f." + dateField
				+ " as char(10)) <= CONVERT(VARCHAR(10),CONVERT(DATETIME,'" + endDate + "'),120)";
		if (rz.equals(Constants.RUZHANG_CODE_YES)) {
			sql = sql + " AND rzDate <> ''";
		} else if (rz.equals(Constants.RUZHANG_CODE_NO)) {
			sql = sql + " AND rzDate = ''";
		}
		List<?> list = super.query(sql);
		return (List<Form>) list;
	}
}
